package C;

import java.util.*;

public class C46 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String  s = sc.nextLine();
        int k = sc.nextInt();
        System.out.println(getResult(s,k));

    }
    static public int getResult(String s,int k) {
        if (k<=-0) return -1;
        HashMap<Character,Integer> map = new HashMap<>();
        int count = 0;
        s = s+"2";
        Character pre = s.charAt(0);
        for (Character c : s.toCharArray()) {
           if (c.equals(pre)) {
               count++;
               pre = c;
           } else {
                map.put(pre,map.getOrDefault(pre,0)>count?
                        map.getOrDefault(pre,0):count);
                pre = c;
                count = 1;
           }
        }
        List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());
        System.out.println(list);
//        list.sort((a,b)->b.getValue()-a.getValue());
        list.sort(Map.Entry.comparingByValue());
        System.out.println(list);
        if (k>list.size()) return -1;
        return list.get(list.size()-k).getValue();

    }
}
